﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.product
{
    public partial class goodsCardType : BaseAdmin
    {
        readonly DALCardType theCardType = new DALCardType();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.AddNewProduct);
                this.BuildCardTypeList();
            }
        }

        private void BuildCardTypeList()
        {
            StringBuilder strHead = new StringBuilder();
            StringBuilder strBody = new StringBuilder();
            StringBuilder strTable = new StringBuilder();
            StringBuilder strCheckedAdmin = new StringBuilder(",");
            DataSet ds = new DataSet();

            if (Request.QueryString["gid"] == null)
            {
                this.btnSave.Visible = false;
                ExtAspNet.Alert.Show("请先添加商品！");
                return;
            }
        
            string sGoodsId = Request.QueryString["gid"];
            ds = theCardType.GetList("");
       
            strTable.Append("<table class='tableRule' border=\"0\" cellpadding=\"3\" cellspacing=\"1\">");
            if (ds.Tables.Count > 0)
            {
               
                strBody.Append("<tr>");
              
                  
                    if (ds.Tables.Count > 0)
                    {
                        strBody.Append("<td valign='top'>");
                        for (int i = 1; i <= ds.Tables[0].DefaultView.Count; i++)
                        {
                       
                            string sChecked = "";
                            if (theCardType.GetGoodsCardType(int.Parse(sGoodsId),int.Parse(ds.Tables[0].DefaultView[i-1]["id"].ToString())))
                            {
                                sChecked = "checked='checked'";
                                strCheckedAdmin.Append(ds.Tables[0].DefaultView[i-1]["id"] + ",");
                            }
                            strBody.Append("<br/><input type='checkbox' onclick='setCardTypeId(this.id)' " + sChecked + " id='" + ds.Tables[0].DefaultView[i-1]["id"].ToString() + "'/>" + ds.Tables[0].DefaultView[i-1]["Name"].ToString());
                            if (i % 10 == 0 && i !=0)
                            {
                                strBody.Append("</td><td valign='top'>");
                            }
                        }

                            strBody.Append("</td>");
          
                    }
                  
   
                strBody.Append("</tr>");
            }
            strTable.Append(strHead.ToString());
            strTable.Append(strBody.ToString());
            strTable.Append("</table>");

            this.ltRuleList.Text = strTable.ToString();
            this.hdList.Value = strCheckedAdmin.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string sCardTypeIds = string.Empty;
            if (this.hdList.Value.Length > 1)
            {
                sCardTypeIds = this.hdList.Value.Substring(1, this.hdList.Value.Length - 2);
            }
            else
            {
                sCardTypeIds = "0,0";
            }
            theCardType.SaveGoodsCardType(int.Parse(Request.QueryString["gid"]), sCardTypeIds);
            ExtAspNet.Alert.Show("保存成功！");
            this.BuildCardTypeList();
        }
    }
}